<%-- 
    Document   : class-modify-action
    Created on : Feb 15, 2012, 10:38:36 PM
    Author     : PhucAn
--%>

<%@page import="dao.ClassDAO"%>
<%@page import="util.PagingUtil"%>
<%@page import="dto.User"%>
<%@page import="util.SharingUtil"%>
<%@page import="dao.ClassStudentDAO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="dao.UserDAO"%>
<%
    UserDAO daoUser = new UserDAO();
    ClassDAO daoCls = new ClassDAO();
    if (request.getParameter("update") != null) {
        dto.Class cls = new dto.Class();
        cls.setName(request.getParameter("name"));
        cls.setIsPublic(request.getParameter("is-public") != null);
        cls.setId(Integer.parseInt(request.getParameter("classid")));
        if (daoCls.update(cls) > 0) {
            SharingUtil.showMessage(session, "Your change is saved");
        } else {
            SharingUtil.showMessage(session, "Error. Action failed");
        }
    }
//process button Add student click    
    if (request.getParameter("add-student") != null) {
        dto.Class cls = new dto.Class();
        ClassStudentDAO daoCS = new ClassStudentDAO();
        ArrayList<dto.User> uList;
        cls.setId(Integer.parseInt(request.getParameter("classid")));

        String emailBundle = request.getParameter("email-list");
        String[] emailList = emailBundle.split(",");
        for (int i = 0; i < emailList.length; i++) {
            emailList[i] = emailList[i].trim();
        }
        uList = daoUser.getUsers(emailList);
        for (dto.User student : uList) {
            cls.getStudents().add(student);
        }
        if (daoCS.create(cls) > 0) {
            SharingUtil.showMessage(session, "Students is added");

        } else {
            SharingUtil.showMessage(session, "Error. Students is not added");
        }
    }




    String pagingNav = "";
    int totalRecords = 0;
    int classId = Integer.parseInt(request.getParameter("classid"));
    int pageSize = PagingUtil.CONST_PAGE_SIZE;
    int currentPage = PagingUtil.getPageNumber(request.getParameter("page"));
    pagingNav = PagingUtil.getPageNavigator(totalRecords, pageSize, currentPage, "?pcontent=modify-class");
    //get student list of class
    ArrayList<User> list = daoUser.getUsers(currentPage, pageSize, "", classId);
    dto.Class currentClass = daoCls.getClass(classId);

    request.setAttribute("list", list);
    request.setAttribute("pagingNav", pagingNav);
    request.setAttribute("cls", currentClass);

%>